﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using QLTV_V2.DataAccess;
using QLTV_V2.BussinessLogic;
using QLTV_V2.BussinessObject;

namespace QLTV_V2.DataAccess
{
    class Utilities
    {
        private string sql;
        private ConnectData conn = new ConnectData();
        private DataTable dataTable;
        //Ham load combobox dung chung
        public DataTable getDataSource(string nameTable, string orderColumn)
        {
                string sql = "select * from " + nameTable + " order by " + orderColumn;
                return conn.GetDataTable(sql);
        }
        public DataTable getDataSource_DK(string nameTable, string nameFiels, string dk, string orderColumn)
        {
            string sql = "select * from " + nameTable + " where " + nameFiels + "= N'" + dk + "' order by " + orderColumn;
            return conn.GetDataTable(sql);
        }
        //Ham delete dùng chung
        public bool deleteTable(string tableName, string columName,string dk)
        {
                string sql = "delete from " + tableName + " where " + columName + " = N'" + dk + "'";
                if (conn.ExecuteQuery(sql) == true)
                    return true;
                return false;

        }
        public bool deleteTale_nocheck(string tableName, string columnName, string dk)
        {
            sql = "alter  table "+tableName+" "+
                  "nocheck  constraint  all "+
                  "delete from " + tableName + " where " + columnName + " = N'" + dk + "'" +
                  "alter  table  " + tableName + " " +
                  "check  constraint  all";
            if (conn.ExecuteQuery(sql) == true)
                return true;
            return false;
        }
        //Lay DS gioi tinh Load combobox
        public DataTable GetGioitinh(string tableName)
        {
            sql = "select DISTINCT GioiTinh, "
                + "NameGT = CASE GioiTinh when 0 then N'Nam' "
                + "when 1 then N'Nữ' END FROM " + tableName;
            return conn.GetDataTable(sql);
        }
        //Lay ma cuối cùng không có đk
        public string GetLastID(string nameTable, string nameSelectColumn, string orderByColumn, string defaul)
        {
            sql = "SELECT TOP 1 * FROM " + nameTable + " ORDER BY " + orderByColumn + " DESC";
            DataTable dtb = conn.GetDataTable(sql);
            try
            {
                return dtb.Rows[0][nameSelectColumn].ToString();
            }
            catch
            {
                return defaul;
            }
        }
        //Lay ma cuối cùng không có đk
        public string GetLastID_DK(string nameTable,string nameColumn_DK,string dk, string nameSelectColumn, string orderByColumn, string defaul)
        {
            sql =   "SELECT TOP 1 * FROM " + nameTable + " "+
                    "where "+nameColumn_DK+" = N'"+dk+"' "+
                    "ORDER BY " + orderByColumn + " DESC";
            DataTable dtb = conn.GetDataTable(sql);
            try
            {
                return dtb.Rows[0][nameSelectColumn].ToString();
            }
            catch
            {
                return defaul;
            }
        }
        //Lấy mã kế tiếp
        public string GetNextID(string lastID, string prefixID)
        {
            if (lastID == "")
            {
                return prefixID + "0001";
            }
            int nextID = int.Parse(lastID.Remove(0, prefixID.Length)) + 1;
            int lengthNumerID = lastID.Length - prefixID.Length;
            string zeroNumber = "";
            for (int i = 1; i <= lengthNumerID; i++)
            {
                if (nextID < Math.Pow(10, i))
                {
                    for (int j = 1; j <= lengthNumerID - i; i++)
                    {
                        zeroNumber += "0";
                    }
                    return prefixID + zeroNumber + nextID.ToString();
                }
            }
            return prefixID + nextID;

        }
        //Hàm dùng chung kiểm tra Email
        public bool isValidEmail(string inputEmail)
        {
            string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
                  @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
                  @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
            Regex re = new Regex(strRegex);
            if (re.IsMatch(inputEmail))
                return (true);
            else
                return (false);
        }
        //Dùng để kiểm tra số
        public bool checkDigit(string s)
        {
            if (s.Length < 1)
                return false;
            for (int i = 0; i < s.Length; i++)
            {
                if (char.IsDigit(s[i]) != true)
                    return false;
            }
            return true;
        }
        //Kiem tra trung khoa
        public bool CheckExitsValue(string nameTable, string nameFile, string value)
        {
            string sql = "select * from " + nameTable + " where " + nameFile + " = N'" + value + "'";
            if (conn.GetDataTable(sql).Rows.Count > 0)
                return false;//Ton tai
            return true;//Khong ton tai
        }
        public bool CheckExitsValue_2dk(string nameTable, string nameFile1, string value1, string nameFile2, string value2)
        {

            string sql = "select * from " + nameTable + " where " + nameFile1 + " = N'" + value1 + "' and " + nameFile2 + "=N'" + value2 + "'";
             MessageBox.Show(sql);
            dataTable = conn.GetDataTable(sql);
            // MessageBox.Show("gt"+ dataTable.Rows.Count.ToString());
            if (dataTable.Rows.Count > 0)
                return true; //TỒN TẠI
            return false; //KHÔNG TỒN TẠI
        }
        
        public bool CheckExitsValue_3dk(string nameTable, string nameFile1, string value1, string nameFile2, string value2, string nameFile3, string value3)
        {

            string sql = "select * from " + nameTable + " where " + nameFile1 + " = N'" + value1 + "' and " + nameFile2 + "=N'" + value2 + "' and "+nameFile3+" =N'"+value3+"' ";
            dataTable = conn.GetDataTable(sql);
            // MessageBox.Show("gt"+ dataTable.Rows.Count.ToString());
            if (dataTable.Rows.Count > 0)
                return true; //TỒN TẠI
            return false; //KHÔNG TỒN TẠI
        }
        //Lấy kiều ngày giờ hệ thống
        public int GetTypeDate()
        {
            string type= System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
            if(type == "MM/dd/yyyy") return 101;
	        if(type == "yyyy.mm.dd") return 102;
            if(type == "dd/MM/yyyy") return 103;
	        if(type == "dd.MM.yyyy") return 104;
	        if(type == "dd-MM-yyyy") return 105;
 	        if(type == "mm-dd-yyyy") return 110;
	        if(type == "yyyy/mm/dd") return 111;
            else return 106;
        }

        //kiem tra
        public bool TimKiem(string[] arr,int n, string value)
        {
            if (n == 0)
                return false;
            else
            {
                int dem = 0;
                for (int i = 0; i < n; i++)
                    if (value == arr[i])
                        return true;
                    else
                        dem++;
                if (dem == 0)
                    return true;
                else
                    return false;
            }
        }

        public void dgv_GetSTT_RowHeader(object sender, DataGridViewRowPostPaintEventArgs e, DataGridView dgv)
        {
            if (e.RowIndex < dgv.Rows.Count - 1)
            {
                string strRowNumber = (e.RowIndex + 1).ToString();
                while (strRowNumber.Length < dgv.RowCount.ToString().Length)
                {
                    strRowNumber = "0" + strRowNumber;
                }
                SizeF Size = e.Graphics.MeasureString(strRowNumber, dgv.Font);

                Brush b = SystemBrushes.ControlText;
                e.Graphics.DrawString(strRowNumber, dgv.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y +
                          ((e.RowBounds.Height - Size.Height) / 2));
            }
        }

    }
}
